﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bigger.Core.Pipeline;

namespace Bigger.Study.ConsoleApp
{
    /// <summary>
    /// 管道模型测试
    /// </summary>
    public class BiggerPipelineExample : IExample
    {
        public void Execute()
        {
            //AlphaHandlerDemo objAlphaHandlerDemo = new AlphaHandlerDemo(
            //    new MessageHandlerDemo(),
            //    new DelegatingHandlerDemo1(),
            //    new DelegatingHandlerDemo2(),
            //    new DelegatingHandlerDemo3()
            //    );
            AlphaHandler objAlphaHandler = new AlphaHandler(
                new MessageHandlerDemo(),
                new DelegatingHandlerDemo1(),
                new DelegatingHandlerDemo2(),
                new DelegatingHandlerDemo3()
            );
            MessageContext objMessageContext = new MessageContext();
            objAlphaHandler.ExecuteMessage(objMessageContext);
        }
    }

    public class DelegatingHandlerDemo1 : DelegatingHandler
    {
        public override void SendMessage(MessageContext objMessageContext)
        {
            Console.WriteLine("DelegatingHandlerDemo1.SendMessage");
        }

        public override void ReceiveMessage(MessageContext objMessageContext)
        {
            Console.WriteLine("DelegatingHandlerDemo1.ReceiveMessage");
        }
    }

    public class DelegatingHandlerDemo2 : DelegatingHandler
    {
        public override void SendMessage(MessageContext objMessageContext)
        {
            Console.WriteLine("DelegatingHandlerDemo2.SendMessage");
            //objMessageContext.Canceled = true;
            //throw new Exception();
        }

        public override void ReceiveMessage(MessageContext objMessageContext)
        {
            Console.WriteLine("DelegatingHandlerDemo2.ReceiveMessage");
            //objMessageContext.Canceled = true;
        }

        public override void OnException(MessageContext objMessageContext)
        {
            base.OnException(objMessageContext);
            //objMessageContext.ExceptionHandled = true;
            //objMessageContext.Canceled = true;
        }
    }

    public class DelegatingHandlerDemo3 : DelegatingHandler
    {
        public override void SendMessage(MessageContext objMessageContext)
        {
            Console.WriteLine("DelegatingHandlerDemo3.SendMessage");
        }

        public override void ReceiveMessage(MessageContext objMessageContext)
        {
            Console.WriteLine("DelegatingHandlerDemo3.ReceiveMessage");
        }
    }

    public class MessageHandlerDemo : MessageHandler
    {
        public override void SendMessage(MessageContext objMessageContext)
        {
            Console.WriteLine("MessageHandlerDemo.SendMessage");
        }

        public override void ReceiveMessage(MessageContext objMessageContext)
        {
            Console.WriteLine("MessageHandlerDemo.ReceiveMessage");
        }

        public override void ExecuteMessage(MessageContext objMessageContext)
        {

        }
    }

    public class AlphaHandlerDemo : AlphaHandler
    {
        public AlphaHandlerDemo(MessageHandler objDispatcherHandler, params DelegatingHandler[] objDelegatingHandlers)
            : base(objDispatcherHandler, objDelegatingHandlers)
        {

        }

        public override void SendMessage(MessageContext objMessageContext)
        {
            Console.WriteLine("AlphaHandlerDemo.SendMessage");
        }

        public override void ReceiveMessage(MessageContext objMessageContext)
        {
            Console.WriteLine("AlphaHandlerDemo.ReceiveMessage");
        }
    }
}
